<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Manage;
use App\Models\ManageLoginLog;
use App\Validate\ManageInfoValidate;
use Illuminate\Http\Request;
use Mews\Captcha\Facades\Captcha;

/**
 * 后台登录界面
 */
class LoginController extends Controller
{

    protected $request = null;
    protected $validateObj = null;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->validateObj = new ManageInfoValidate();

    }

    /**
     * 后台管理员登录
     * @param account 账号  
     * @param password 密码  
     * @param captcha string  验证码  默认  5位
     * @param key_str  string 验证码字符串
     */
    public function login(){
      //  dump(request()->session()->all());
        //增加验证场景进行验证
        if (!$this->validateObj->scene('login')->check($this->request->all())) {
            return response()->json(['code'=>201,  'msg'=>$this->validateObj->getError()]);
        }
         //验证密码
         $validate_password = validate_password($this->request->password);
         if ($validate_password !== true) {
            return response()->json(['code'=>201,  'msg'=>$validate_password]);
         }
         
        //再次验证验证码是否正确，验证器里面没有验证
        if (!captcha_api_check($this->request->captcha, $this->request->key_str,'flat')){
            return response()->json(['code'=>201,  'msg'=>'验证码输入错误']);
        }

        $res = Manage::where('account' , $this->request->account)->where('is_del',1)->first();
        if(empty($res)){
            return response()->json(['code'=>202,  'msg'=>'此账号不存在']);
        }

        if($res->password != md5($this->request->password)){
            return response()->json(['code'=>202,  'msg'=>'密码输入不正确']);
        }

         /*生成token*/
         $tokenInfo = $this->getTokenExpireTime();
         $res->token = $tokenInfo['token'];
         $res->expire_time = $tokenInfo['expire_time'];
         $res->end_login_time = date('Y-m-d H:i:s');
         $res->end_login_ip = request()->ip();;
         $result = $res->save();
 
         if(!$result){
             return response()->json(['code'=>202,  'msg'=>'登录失败,请稍后重试']);
         }


        //判断是否是初始密码  账号  加上 123 就是初始密码
        if($res->password == $res->account.'123'){
            $is_modify_pwd = true;//初始密码，需要修改
        }else{
            $is_modify_pwd = false;//不是初始密码，需要修改
        }

         //添加登录日志
         ManageLoginLog::insertLoginLog($res->id ,  $res->end_login_ip);

         $tokenInfo['is_modify_pwd'] = $is_modify_pwd;//返回是否需要修改密码
         $tokenInfo['account'] = $res['account'];//返回是否需要修改密码
         return response()->json(['code'=>200,  'msg'=>'登录成功','content'=>$tokenInfo]);
    }

 


    /**
     * 获取图形验证码   (需要现在中间件中开始session，不然会一直验证不通过)
     */
    public function getCaptcha(){
    //     $data = Captcha::src('flat');
    //   //  $data = Captcha::src();

    //     return response()->json(['code'=>200,  'msg'=>'获取成功','content'=>['captcha_url'=>$data]]);

        return response()->json(['code'=>200,  'msg'=>'获取成功','content'=>Captcha::create('flat', true)]);//flat 要与 验证器captcha里面的 值一致 

    
    }

    /**
     * 获取用户token 及失效时间
     */
    public function getTokenExpireTime(){
        $data['token'] = getGuid();
        $data['expire_time'] = date("Y-m-d H:i:s",(time() + config('other.admin_token_refresh_time')));
        return $data;
    }




}
